package com.zzptc.easyshop.dao;

import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.vo.News;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDao implements IDAO{
    @Override
    public boolean insert(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean login(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean delete(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean updata(VO vo) throws SQLException {
        return false;
    }

    @Override
    public List getAll() throws SQLException {

        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        conn=db.getConn();
        ps=conn.prepareStatement("select * from easybuy_news");
        rs=ps.executeQuery();
        List list=new ArrayList();
        while (rs.next()){
            News news=new News();
            news.setEn_id(rs.getInt(1));
            news.setEn_title(rs.getString(2));
            news.setEn_content(rs.getString(3));
            news.setEn_create_time(rs.getString(4));
            list.add(news);
        }
        db.closeAll(conn,ps,rs);
        return list;
    }
    public News getByid(int id) throws SQLException {
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

            conn=db.getConn();
            ps=conn.prepareStatement("select * from easybuy_news where en_id =?");
            ps.setInt(1,id);
            rs=ps.executeQuery();
            News news=new News();
            if (rs.next()){
            news.setEn_id(rs.getInt(1));
            news.setEn_title(rs.getString(2));
            news.setEn_content(rs.getString(3));
            news.setEn_create_time(rs.getString(4));

        }
        //结束所有
        db.closeAll( conn,ps,rs );
        return news;
    }

    public static void main(String[] args) {
        NewsDao newsDao=new NewsDao();
        try {
            System.out.println(newsDao.getAll().size());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
